<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>websocket demo</title>
    <link rel="shortcut icon" href="/resource/img/favicon.ico">
    <!-- Bootstrap -->
    <link href="/resource/css/bootstrap.min.css" rel="stylesheet">
    <link href="/resource/css/flat-ui.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/resource/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/resource/css/home.css"/>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="/resource/js/vendor/html5shiv.js"></script>
    <script src="/resource/js/vendor/respond.min.js"></script>
    <![endif]-->
    <script src="/resource/js/cufon-yui.js" type="text/javascript"></script>
    <script src="/resource/js/ChunkFive_400.font.js" type="text/javascript"></script>
    <script type="text/javascript">
        Cufon.replace('.intro>h1', {color: '#fff', textShadow: '0 1px 2px rgba(0, 0, 0, 0.3)'});
        Cufon.replace('.navbar-brand', {textShadow: '0 1px 2px rgba(0, 0, 0, 0.3)'});

    </script>
</head>
<body class="weixin">
<div id="weixin">
    <% include ../Home/include/nav.html%>
</div>
<div class="container top36 bot40">
    <div class="panel panel-default">
        <div class="panel-body ">
            <div class="data-list" style="background:#efefef;height:400px;overflow:auto; padding: 10px"></div>
            <form role=form class="form-inline" style="margin-top:10px" onsubmit="return false">
                <textarea  class="form-control" id=textField style="width:100%; margin-bottom: 10px" ></textarea>
                <button type=submit class="btn btn-primary btn-block" id=submitBtn>发送</button>
            </form>
        </div>
    </div>
</div>
<% include ../Home/include/copyright.html%>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="/resource/js/jquery.min.js"></script>
<script src="/resource/js/jquery.particleground.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/resource/js/flat-ui.min.js"></script>
<script src="/resource/js/application.js"></script>
<script>
    $(function () {
        $('#weixin').particleground({
            dotColor: '#53DBC7',
            lineColor: '#29C7AF'
        });
        var htmlMaps = {
            '<': '<',
            '>': '>',
            '"': '&quote;',
            "'": '&#39;'
        }
        var escape_html = function (str) {
            return (str + "").replace(/[<>'"]/g, function (a) {
                return htmlMaps[a];
            })
        }

        function getWebSocket() {
            var socket = new WebSocket("ws://" + location.hostname + ":8360/home/websocket/open");
            var deferred = $.Deferred();
            // 打开Socket
            socket.onopen = function (event) {
                $('<div class="alert alert-success alert-dismissible  fade in"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> 连接成功，开始聊天吧 ^_^</div>').appendTo(dataList);
                deferred.resolve(socket);
                // 监听消息
                socket.onmessage = function (event) {
                    var data = JSON.parse(event.data).result;
                    console.log(data);
                    var html = '<div class="well well-sm"><i class="fa fa-github-alt fa"></i> <span class="label label-primary">' + data.name + '</span> '+ data.ip;
                    html += '<p>' + escape_html(data.text) + '</p></div>'
                    $(html).appendTo(dataList);
                    dataList[0].scrollTop = 100000000;
                };
                // 监听Socket的关闭
                socket.onclose = function (event) {
                    socket = null;
                    console.log("websocket closed")
                };
            };
            return deferred;
        }

        var ws = getWebSocket();
        var dataList = $('.data-list');
        var input = $('#textField').select();
        $("#textField").keydown(function(e){
            if(e.keyCode == 13){
                $("#submitBtn").trigger("click");
            }
        });

        $('#submitBtn').click(function () {
            var value = input.val().trim();
            if (!value) {
                return input.focus();
            }
            ;
            ws.then(function (ws) {
                input.val('').focus();
                ws.send(JSON.stringify({
                    jsonrpc: "2.0",
                    method: "/home/websocket/message",
                    params: {text: value},
                    id: 1
                }))
            })
        })
    })
</script>
</body>
</html>